package com.recyclerview422zfj.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.recyclerview422zfj.R;
import com.recyclerview422zfj.adapter.CourseAdapter;
import com.recyclerview422zfj.bean.CourseBean;

import java.util.List;

/**
 * Created by zengfanjun on 2017/5/23.
 */

public class MyFragment1 extends Fragment {
    private View view = null;
    private Context context;
    private PagerSlidingTabStrip pagersliding;
    private ViewPager viewpager;
    private CollectArticleFragment1 collectArticleFragment1;
    private CollectCourseFragment1 collectCourseFragment1;
    private CollectVideoFragment1 collectVideoFragment1;
    private DisplayMetrics dm;

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private CourseAdapter CourseAdapter;
    private List<CourseBean> list = null;
    private static int cur_page = 1;
    private static int lastVisibleItemPosition;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        //动态加载Fragment3的布局文件
        view = inflater.inflate(R.layout.myfragment, container, false);
        init();
        //返回动态生成的view
        return view;

    }

    private void init() {
        dm = getResources().getDisplayMetrics();//获取屏幕密度
        viewpager = (ViewPager) view.findViewById(R.id.viewPager);
        viewpager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        pagersliding = (PagerSlidingTabStrip) view.findViewById(R.id.pagerslidingtabstrip);
        pagersliding.setViewPager(viewpager);
        setpagerstyle();//设置PagerSlidingTabStrip显示效果

    }

    private void setpagerstyle() {
        pagersliding.setShouldExpand(true); // 设置Tab是自动填充满屏幕的
        pagersliding.setDividerColor(Color.TRANSPARENT); // 设置Tab的分割线是透明的
        // 设置Tab底部线的高度
        pagersliding.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        pagersliding.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));
        // 设置Tab标题文字的大小
        pagersliding.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm));
        pagersliding.setIndicatorColor(Color.parseColor("#45c01a"));// 设置Tab Indicator的颜色
        // 设置选中Tab文字的颜色 (这是自定义的一个方法)
        pagersliding.setSelectedTextColor(Color.parseColor("#45c01a"));
        pagersliding.setTabBackground(0); // 取消点击Tab时的背景色

    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        private String[] titles = {"文章", "课程","视频"};//显示在二级导航上的标题文字

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (collectArticleFragment1 == null) {
                        collectArticleFragment1 = new CollectArticleFragment1();
                    }
                    return collectArticleFragment1;
                case 1:
                    if (collectCourseFragment1 == null) {
                        collectCourseFragment1 = new CollectCourseFragment1();
                    }
                    return collectCourseFragment1;
                case 2: if (collectVideoFragment1 == null) {
                    collectVideoFragment1 = new CollectVideoFragment1();
                }
                return collectVideoFragment1;
                default:
                    return null;
            }


        }
        @Override
        public CharSequence getPageTitle ( int position){
            return titles[position];//确定当页导航上文字


        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }



    }